package cn.ococci.dm.Main.Core;

import com.alibaba.fastjson.Json;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.ococci.dm.Main.Const;
import cn.ococci.dm.Main.OtaType;
import cn.ococci.dm.Main.Tools.LogTool;


/**
 * Created by cyan on 15/12/29.
 */
public class EncodeCommandUtils {

    public static final String TAG = "EncodeCommandUtils";


    public static String builderWhiteList(List<Integer> ids, boolean isWhite){

        int size = ids.size();

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {

            sb.append(ids.get(i));

            if (i != size - 1){
                sb.append(",");
            }
        }
        Map<String, Object> tmpMap = new HashMap<>();
        tmpMap.put(Const.CommandParams.COMMAND_TYPE,isWhite? Const.CommandParams.COMMAND_SET_WHITE_LIST : Const.CommandParams.COMMAND_SET_BLACKLIST);
        tmpMap.put(Const.BuilderParams.card_list, sb.toString());

        String detail = Json.toJSONString(tmpMap);

        LogTool.log(TAG, "构建的新增名单数据是 :" + detail);
        return detail;
    }


    /**
     * 请求固件版本号
     * @return
     */
    public static String builderOTAVersion(){

        Map<String, Object> versionMap = new HashMap<>();
        versionMap.put(Const.CommandParams.COMMAND_TYPE,Const.CommandParams.COMMAND_REQUEST_VERSION);

        String requestVersion = Json.toJSONString(versionMap);

        LogTool.log(TAG, "构建的请求固件版本号 :" + requestVersion);

        return requestVersion;

    }

    /**
     * 构建升级ota请求数据
     * @param type
     * @param localIp
     * @param port
     * @return
     */
    public static String builderUpgradeWithOta(OtaType type, String localIp, int port){

        Map<String,Object> map = new HashMap<>();

        if(type == OtaType.OTC_AP){
            map.put(Const.CommandParams.COMMAND_TYPE,Const.CommandParams.COMMAND_UPGRADE_AP);
        }else {
            map.put(Const.CommandParams.COMMAND_TYPE,Const.CommandParams.COMMAND_upgrade_PR);
        }

        map.put(Const.CommandParams.COMMAND_IP,localIp);
        map.put(Const.CommandParams.COMMAND_PORT,""+port);

        String upgrade = Json.toJSONString(map);

        LogTool.log(TAG,"升级命令操作:" + upgrade);

        return upgrade;
    }



    /**
     * 设置红外开关
     * @return
     */
    public static String builderOpenInfrared(boolean isOn){

        Map<String, Object> versionMap = new HashMap<>();
        versionMap.put(Const.CommandParams.COMMAND_OPEN_INFRARED,isOn?Const.CommandParams.COMMAND_OPEN:Const.CommandParams.COMMAND_CLOSE);

        String requestVersion = Json.toJSONString(versionMap);

        LogTool.log(TAG, "构建的打开红外 :" + requestVersion);

        return requestVersion;

    }



    /**
     * 获取红外状态
     * @return
     */
    public static String builderInfraredStatus(){

        Map<String, Object> versionMap = new HashMap<>();
        versionMap.put(Const.CommandParams.COMMAND_TYPE,Const.CommandParams.COMMAND_INFRARED_STATUS);

        String requestVersion = Json.toJSONString(versionMap);

        LogTool.log(TAG, "构建的打开红外状态 :" + requestVersion);

        return requestVersion;

    }









}
